package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.User;
import com.example.mapper.RoleMapper;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import com.example.vo.LoginUser;
import com.example.vo.RegisterUser;
import com.example.vo.ResetPasswordVO;
import com.example.vo.UserInfoVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
* @author circle48
* @description 针对表【user】的数据库操作Service实现
* @createDate 2024-05-14 20:25:44
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Resource
    private BCryptPasswordEncoder passwordEncoder;
    @Resource
    private RoleMapper roleMapper;

    @Override
    public boolean existsByPhone(String phone) {
        return userMapper.existsByPhone(phone) != null;
    }

    @Override
    public boolean resetPassword(ResetPasswordVO user){
        return userMapper.resetPassword(user) >0;
    }

    @Override
    public Long getIdByPhone(String phone){
        return userMapper.getIdByPhone(phone);
    }

    @Override
    public boolean register(RegisterUser user){
        return userMapper.register(user) > 0;
    }

    @Override
    public void addUser(User entity) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String encodePassword = passwordEncoder.encode(entity.getPassword());
        entity.setPassword(encodePassword);
        entity.setCreateBy(loginUser.getUser().getPhone());
        entity.setCreateTime(LocalDateTime.now());

        userMapper.insert(entity);
        //赋予默认权限
        Long userId = entity.getId();
        roleMapper.insertDefaultRole(userId);
    }


    @Override
    public void updataUserIntegration(int totalPoints, User u) {
        baseMapper.updataUserIntegration(totalPoints,u.getId());
    }

    @Override
    public UserInfoVo getUserInfo(Long userId){
        UserInfoVo userInfoVo = baseMapper.getUserInfoById(userId);
        String roleName = roleMapper.getRoleName(userId);
        userInfoVo.setRoleName(roleName);
        return userInfoVo;
    }

    @Override
    public boolean updateProfile(Long userId, String avatar){
        return baseMapper.updateProfile(userId, avatar)>0;
    }

    @Override
    public boolean updatePassword(Long userId, String password){
        String encodePassword = passwordEncoder.encode(password);
        return baseMapper.updatePassword(userId, encodePassword)>0;
    }

    @Override
    public boolean updateUserInfo(Long userId, String nickname){
        return baseMapper.updateUserInfo(userId, nickname)>0;
    }
}




